//
//  SFViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/5/10.
//

#import "SFViewController.h"

@interface SFViewController ()

@end

@implementation SFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"算法";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //排序两个有序数组
//    NSArray *arrayA = @[@1,@15,@20];
//    NSArray *arrayB = @[@8,@13,@20,@22];
//    NSArray *comArray = [self sortArray:[arrayA mutableCopy] withArray:[arrayB mutableCopy]];
//    NSLog(@"-----%@",comArray);
 
    
    //交换a和b
//    [self changeNum:5 with:6];
    
    //最大公约数
//    [self maxCommonDivisor:36 with:18];
    
    
//    NSMutableArray *mArray = [NSMutableArray array];
//    for (int i = 0; i < 100; i++) {
//        NSInteger n = random()%100;
//        [mArray addObject:[NSNumber numberWithInteger:n]];
//    }
//    [self StatisticsNum:mArray];
    
    
    [self addTotal:@[@2,@7,@11,@15] tar:18];
    
}



///排列两个有序数组
/// @param arrA 有序数组1
/// @param arrB 有序数组2
-(NSArray *)sortArray:(NSMutableArray *)arrA withArray:(NSMutableArray *)arrB{
    
    if (arrA.count == 0 && arrB.count == 0) {
        return @[];
    }
    if (arrA.count == 0) {
        return arrB;
    }
    
    if (arrB.count == 0) {
        return arrA;
    }
    
    NSMutableArray *mutaArray = [NSMutableArray array];

    while (1) {
        
        if([arrA[0] intValue] < [arrB[0] intValue]) {
            
            [mutaArray addObject:arrA[0]];
            [arrA removeObject:arrA[0]];

        }else{
            
            [mutaArray addObject:arrB[0]];
            [arrB removeObject:arrB[0]];
        }
        
        if (arrA.count == 0) {
            [mutaArray addObjectsFromArray:arrB];
            break;
        }
        
        if (arrB.count == 0) {
            [mutaArray addObjectsFromArray:arrA];
            break;
        }
    }
    return mutaArray;
    
}


//交换a和b的值
-(void)changeNum:(NSInteger )a with:(NSInteger)b{
    
    //方法1
//    NSInteger c = b;
//    b = a;
//    a = c;
//    NSLog(@"a---%ld,b----%ld",(long)a,(long)b);
    
    a = a + b;
    b = a - b;
    a = a - b;
    NSLog(@"a---%ld,b----%ld",(long)a,(long)b);
    
    
    
}

//最大公约数
-(void)maxCommonDivisor:(int)a with:(int)b{
    
    //直接遍历法
    int max = 0;

    for (int i = 1; i <= b; i++) {

        if (a % i == 0 && b % i == 0) {
            max = i;
        }
    }
    NSLog(@"最大公约数为%d",max);
        
    // 扩展：最小公倍数 = (a * b)/最大公约数
    
}



///  一个范围在0-100的整数数组,统计每个数字个数
/// @param array 整数数组
-(void)StatisticsNum:(NSArray *)array{
    
    NSDictionary *dic = [NSMutableDictionary dictionary];
    
    for (NSNumber *num in array) {
        
        NSString *key = [num stringValue];
        
        if ([dic objectForKey:key]) {
            
            NSNumber *value = [dic objectForKey:key];
            NSInteger newValue = [value integerValue] + 1;
            NSNumber *newNum = [NSNumber numberWithInteger:newValue];
            [dic setValue:newNum forKey:key];
        }else{
            [dic setValue:@1 forKey:key];
        }
    }
    
    int i = 0;
    for (NSString *key in dic.allKeys) {
        NSNumber *num = [dic objectForKey:key];
        i = i + [num intValue];
        NSLog(@"数字%@---出现次数%@",key,num);
    }
    NSLog(@"总次数为%d",i);
}


-(void)addTotal:(NSArray *)arr tar:(int)target{
    
    for (int i = 0; i<arr.count; i++) {
        int f = [arr[i] intValue];
        for (int j = 0; j < arr.count; j++) {
            int s = [arr[j] intValue];
            if (s + f == target && i != j) {
                NSLog(@"f的值为%d，f的坐标为%d",f,i);
                NSLog(@"s的值为%d，s的坐标为%d",s,j);
                break;
            }
            
        }
        
    }
    
}

@end
